package gao.xiaolei.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import javax.annotation.Resource;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import gao.xiaolei.dto.TestSystemDto;
import gao.xiaolei.entity.ResponseEnvelope;
import gao.xiaolei.entity.TestSystem;
import gao.xiaolei.entity.TestSystemAttached;
import gao.xiaolei.exception.AttachedException;
import gao.xiaolei.exception.DevManException;
import gao.xiaolei.service.TestSystemService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/testSystem")
public class TestSystemController {

    @Resource(name = "TestSystemService")
    private TestSystemService testSystemService;

    @Resource(name = "GsonUtil")
    private Gson gson;

    @PostMapping("")
    public void addTestSystem(@RequestParam("id") String id,
                              @RequestParam("name") String name,
                              @RequestParam("type") Integer type,
                              @RequestParam("describe") String describe,
                              @RequestParam("addMan") String addMan,
                              @RequestParam(value = "devMan", required = false) String devMan,
                              @RequestParam("attachedList") String attachedList,
                              @RequestParam("attachedFiles") MultipartFile[] attachedFiles) throws IOException {
        testSystemService.addTestSystem(id, name, type, describe, addMan, devMan, gson.fromJson(attachedList, new TypeToken<List<TestSystemAttached>>() {
        }.getType()), attachedFiles);
    }

    @PutMapping("")
    public void updateTestSystem(@RequestParam("id") String id,
                                 @RequestParam("name") String name,
                                 @RequestParam("type") Integer type,
                                 @RequestParam("describe") String describe,
                                 @RequestParam("addMan") String addMan,
                                 @RequestParam(value = "devMan", required = false) String devMan,
                                 @RequestParam("attachedList") String attachedList,
                                 @RequestParam("attachedFiles") MultipartFile[] attachedFiles,
                                 @RequestParam("currentUserId") String currentUserId) throws IOException {
        testSystemService.updateTestSystem(id, name, type, describe, addMan, devMan, gson.fromJson(attachedList, new TypeToken<List<TestSystemAttached>>() {
        }.getType()), attachedFiles,currentUserId);
    }

    @DeleteMapping("/{id}")
    public void deleteTestSystem(@PathVariable("id") String id) {
        testSystemService.deleteTestSystem(id);
    }

    @GetMapping("/detail/{id}")
    public TestSystemDto getDetail(@PathVariable("id") String id) {
        return testSystemService.getSystemDetail(id);
    }

    @GetMapping("/all")
    public List<TestSystem> getAll() {
        return testSystemService.getSystemList();
    }

    @GetMapping("/attached/{id}")
    public ResponseEntity<byte[]> downAttached(@PathVariable("id") String id) throws UnsupportedEncodingException {
        return testSystemService.downloadAttached(id);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ResponseEnvelope<String>> handelError(Exception e) {
        e.printStackTrace();
        Integer code = null;
        if (e.getClass() == MissingServletRequestParameterException.class)
            code = 400;
        else if (e.getClass() == AttachedException.class)
            code = 406;
        else if (e.getClass() == DevManException.class)
            code = 407;
        else
            code = 500;
        ResponseEnvelope<String> result = new ResponseEnvelope<String>(code, e.getMessage(), null);
        return new ResponseEntity<ResponseEnvelope<String>>(result, HttpStatus.valueOf(500));
    }
}
